package com.cruise.system.userhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cruise.system.userapi.entity.SysRole;
import com.cruise.system.userapi.entity.SysRoleUser;
import com.cruise.system.userhandle.mapper.SysRoleMapper;
import com.cruise.system.userhandle.mapper.SysRoleUserMapper;
import com.cruise.system.userhandle.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭闯
 * @since 2021-01-13
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleUserMapper sysRoleUserMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;


    @Override
    public List<SysRole> getRoleByUserId(String id) {
        List<SysRoleUser> sysUserRoles = sysRoleUserMapper.selectList(new QueryWrapper<SysRoleUser>().lambda().eq(SysRoleUser::getUserId, id));
        List<String> roleIds = sysUserRoles.stream().map(SysRoleUser::getRoleId).collect(Collectors.toList());
        List<SysRole> sysRoles = sysRoleMapper.selectList(new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds));
        return sysRoles;
    }
}
